﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.ObjectModel;
using Ones.Models;

using TweetSharp.Twitter.Service;
using TweetSharp.Twitter.Model;
using TweetSharp;
using TweetSharp.Model;

using System.Diagnostics;
using System.IO;
using System.Net;


namespace Ones.ViewModels
    {
    class DirectMessageViewModel
        {
            private ObservableCollection<TwitterDirectMessage> _directMessages = new ObservableCollection<TwitterDirectMessage>();

            public ObservableCollection<TwitterDirectMessage> DirectMessages
            {
                get { return _directMessages; }
            }

            private void CreateFirstDMs(IEnumerable<TwitterDirectMessage> directMessages)
            {
                foreach (var dm in directMessages)
                {
                    _directMessages.Add(dm);
                }
            }

            private void InsertLastDMs(IEnumerable<TwitterDirectMessage> directMessages)
            {
                foreach (var dm in directMessages)
                {
                    _directMessages.Insert(0, dm);
                }
            }

            public void LoadDMs(TwitterService service)
            {
                IEnumerable<TwitterDirectMessage> directMessages;

                if (_directMessages.Count == 0)
                {
                    directMessages = service.ListDirectMessagesReceived();
                    CreateFirstDMs(directMessages);
                }
                else
                {
                    TwitterDirectMessage last = _directMessages.ElementAt<TwitterDirectMessage>(0);
                    directMessages = service.ListDirectMessagesReceivedSince(last.Id);
                    InsertLastDMs(directMessages);
                }

            }
        }
    }
